/**
 * Created by zzy on 2016/12/8.
 */
var $ = require('jquery');
require('easyui');
require('layout/uiJs');
require('js-path/jquery-validation-wrapper');
var $alert = require("libs/alert");
var $confirm = require("libs/confirm");
var modifyCreditAmountValidator = null;
var isSale = require('libs/user-type').isSale;
$(function () {
    $('#ft').show();
    $.validator.addMethod("maxDecimalPoint",function(value, element){
        return this.optional(element) || /^[-]?\d+(\.\d{1,2})?$/.test(value)
    },"小数点后最多为两位");
    $('#search').on('click',function () {
        var formData = $('#searchForm').serializeObject();
        $('#accountTable').datagrid('load',formData);
    });
    $(document).on('click','a[name=entDetail]',function () {
        window.parent.tabOpen($(this).attr('ref'),'客户公司',true,true);
    });
    $(document).on('click','.handler',function (e) {
        var $this = $(this);
        var scene = $this.attr('name');
        if(scene == 'modifyCreditAmount'){
            var full = $this.data('full');
            $('#modifyCreditAmountModal').modal('show',{
                id: full.id,
                entId: full.entId,
                entName: full.entName,
                creditAmount: full.creditAmount
            });
        }else{
            return false;
        }
    });
    $('#modifyCreditAmountModal').on('show.bs.modal',function (e) {
        var rel = e.relatedTarget;
        $('#mModifyCreditAmountForm').setForm(rel);
    });
    $('#modifyCreditAmountModal').on('hidden.bs.modal',function (e) {
        $('#mModifyCreditAmountForm').find('input').val('').end().find('p').text('');
        modifyCreditAmountValidator.resetForm();
    });
    $('#mModifyCreditAmountCommit').on('click',function (e) {
        $('#mModifyCreditAmountForm').submit();
    });
    modifyCreditAmountValidator = $('#mModifyCreditAmountForm').validateWapper({
        rules: {
            creditAmount: {
                required: true,
                number: true,
                maxDecimalPoint: true,
                min: 0.01
            }
        },
        messages: {
            creditAmount: {
                required: '必填',
                number: '要求数字格式',
                min: '要求大于0'
            }
        },
        submitHandler: function() {
            var formData = $('#mModifyCreditAmountForm').serializeObject();
            $.ajax({
                url: 'api/v1/p/mms/account/updateCredit',
                type: 'post',
                data: JSON.stringify(formData),
                contentType: 'application/json;charset=utf-8'
            })
                .done(function (res) {
                    if(res.code==200){
                        $('#modifyCreditAmountModal').modal('hide');
                        $('#accountTable').datagrid('reload');
                    }else{
                        $alert(res.message);
                    }
                });
        }
    });
    tableInit();
    function tableInit() {
        $('#accountTable').datagrid({
            url:'api/v1/p/mms/account/list',
            border : false,
            height:$(window).height(),
            pagination : true,
            pageSize : 20,
            pageList : [ 20, 50,  100 ],
            emptyMsg : "暂无数据",
            singleSelect: true,
            rownumbers:true,
            columns:[[
                {
                    field:'entName',
                    title:'客户名称',
                    width:'13%',
                    formatter:function (value,row,index) {
                        return "<a class='ellipsis' href='javascript:;' ref='company-detail?id="+row.entId+"' name='entDetail' title='"+value+"'>"+value+"</a>";
                    }
                },
                {
                    field:'billType',
                    title:'类型',
                    width:'8%',
                    formatter:function (value,row,index) {
                        switch (value){
                            case 'pre': return '预付';
                            case 'post': return '后付费';
                            default: return '';
                        }
                    }
                },{
                    field:'billStatus',
                    title:'状态',
                    width:'8%',
                    formatter:function (value,row,index) {
                        switch (value){
                            case 'active': return '活跃';
                            case 'frozen': return '冻结';
                            case 'new': return '新建且未认证';
                            default: return '';
                        }
                    }
                },{
                    field:'isfreeze',
                    title:'是否冻结',
                    width:'8%',
                    formatter:function (value,row,index) {
                        switch (value){
                            case 'Y': return '冻结';
                            case 'N': return '未冻结';
                            default: return '';
                        }
                    }
                },{
                    field:'currency',
                    title:'货币类型',
                    width:'8%'
                },{
                    field:'balance',
                    title:'余额',
                    width:'9%'
                },{
                    field:'frozenBalance',
                    title:'冻结金额',
                    width:'9%'
                },{
                    field:'availableBalance',
                    title:'可用余额',
                    width:'9%',
                    formatter:function (value,row,index) {
                        if(value < 1000) {
                            return "<span style='color: red;'>"+value+"</span>"
                        }
                        return value;
                    }
                },{
                    field:'creditGrade',
                    title:'信用等级',
                    width:'9%'
                },{
                    field:'creditAmount',
                    title:'信用额度',
                    width:'9%'
                },{
                    field:'opt',
                    title:'操作',
                    hidden:isSale,
                    width:'10%',
                    formatter:function (value,row,index) {
                        return "<a class='handler' href='javascript:;' name='modifyCreditAmount' data-full='"+JSON.stringify(row)+"'>修改信用额度</a>"
                    }
                }
            ]],
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            },
            toolbar:"#tb"
        });
    }
});
